-- Zoom library v1.5
-- 缩放移动
Zoom = class()

function Zoom:init(x,y,minScale,maxScale)
    self.touches = {}
    self.initx = x or 0
    self.inity = y or 0
    self.minScale = minScale or 0.1
    self.maxScale = maxScale or 10
    self.couldZoom = false
    self:clear()
    -- self:readLocalData()
end

function Zoom:saveLocalData()
    saveLocalData("Zoom_center_x",self.center.x)
    saveLocalData("Zoom_center_y",self.center.y)
    saveLocalData("Zoom_offset_x",self.offset.x)
    saveLocalData("Zoom_offset_y",self.offset.y)
    saveLocalData("Zoom_zoom",self.zoom)
end

function Zoom:readLocalData()
    self.center.x=readLocalData("Zoom_center_x",self.center.x) or self.center.x
    self.center.y=readLocalData("Zoom_center_y",self.center.y) or self.center.y
    self.offset.x=readLocalData("Zoom_offset_x",self.offset.x) or self.offset.x
    self.offset.y=readLocalData("Zoom_offset_y",self.offset.y) or self.offset.y
    self.zoom=readLocalData("Zoom_zoom",self.zoom) or self.zoom
end

function Zoom:clear()
    self.lastPinchDist = 0
    self.pinchDelta = 1.0
    self.center = vec2(self.initx,self.inity)
    self.offset = vec2(0,0)
    self.zoom = 1
    self.started = false
    self.started2 = false
end

function Zoom:touched(touch)
    if touch.state == ENDED then
        self.touches[touch.id] = nil
        -- self:saveLocalData()
    else
        self.touches[touch.id] = touch
        
        --    if (touch.tapCount==2) then
        --           self:clear()
        --      end
    end
    local newTouch = {}
    newTouch.id = touch.id
    newTouch.state = touch.state
    newTouch.tapCount = touch.tapCount
    newTouch.x,newTouch.y = self:getWorldPoint(vec2(touch.x,touch.y)):unpack()
    newTouch.prevX,newTouch.prevY = self:getWorldPoint(vec2(touch.prevX,touch.prevY)):unpack()
    newTouch.deltaX,newTouch.deltaY = touch.deltaX * self.zoom,touch.deltaY * self.zoom
    
    touch = newTouch
end

function Zoom:processTouches()
    local touchArr = {}
    for k,touch in pairs(self.touches) do
        -- push touches into array
        table.insert(touchArr,touch)
    end
    
    if #touchArr == 2 then
        self.started = false
        local t1 = vec2(touchArr[1].x,touchArr[1].y)
        local t2 = vec2(touchArr[2].x,touchArr[2].y)
        
        local dist = t1:dist(t2)
        if self.started2 then
            --if self.lastPinchDist > 0 then
            self.pinchDelta = dist/self.lastPinchDist
        else
            self.offset= self.offset + ((t1 + t2)/2-self.center)/self.zoom
            self.started2 = true
        end
        self.center = (t1 + t2)/2
        self.lastPinchDist = dist
    elseif (#touchArr == 1) then
        self.started2 = false
        local t1 = vec2(touchArr[1].x,touchArr[1].y)
        self.pinchDelta = 1.0
        self.lastPinchDist = 0
        if not(self.started) then
            self.offset = self.offset + (t1-self.center)/self.zoom
            self.started = true
        end
        self.center=t1
    else
        self.pinchDelta = 1.0
        self.lastPinchDist = 0
        self.started = false
        self.started2 = false
    end
end

function Zoom:clip(x,y,w,h)
    clip(x*self.zoom+self.center.x- self.offset.x*self.zoom,
    y*self.zoom+self.center.y- self.offset.y*self.zoom,
    w*self.zoom+1,h*self.zoom+1)
end

function Zoom:text(str,x,y)
    local fSz = fontSize()
    local xt=x*self.zoom+self.center.x- self.offset.x*self.zoom
    local yt=y*self.zoom+self.center.y- self.offset.y*self.zoom
    fontSize(fSz*self.zoom)
    local xtsz,ytsz=textSize(str)
    tsz=xtsz
    if tsz<ytsz then tsz=ytsz end
    if (tsz>2048) then
        local eZoom= tsz/2048.0
        fontSize(fSz*self.zoom/eZoom)
        pushMatrix()
        resetMatrix()
        translate(xt,yt)
        scale(eZoom)
        text(str,0,0)
        popMatrix()
        fontSize(fSz)
    else
        pushMatrix()
        resetMatrix()
        fontSize(fSz*self.zoom)
        text(str,xt,yt)
        popMatrix()
        fontSize(fSz)
    end
end

function Zoom:scaleDraw()
    -- compute pinch delta
    if self.couldZoom then
        self:processTouches()
    end
    -- scale by pinch delta
    self.zoom = math.max(self.zoom * self.pinchDelta, self.minScale)
    self.zoom = math.min(self.zoom, self.maxScale)
    
    local pa = self:getLocalPoint(vec2(0,0))
    local pb = self:getLocalPoint(vec2(WIDTH,HEIGHT))
    if pa.y > 0 then
        self.center.y = self.offset.y * self.zoom
        
        if self.zoom >= self.maxScale then
        self.center.y = self.offset.y * self.zoom
    else
        self.zoom = self.center.y / self.offset.y
    end
    
    end
    
    if pa.x > 0 then
    self.center.x = self.offset.x * self.zoom
end
    

    -- print(pb.y,self.center.y)
    translate(self.center.x - self.offset.x*self.zoom,
    self.center.y - self.offset.y*self.zoom)
    -- translate(self.center.x - self.offset.x*self.zoom,0)
    
    scale(self.zoom,self.zoom)
    
    self.pinchDelta = 1.0
end

function Zoom:getWorldPoint(pt)
    return self.offset - (self.center - pt) / self.zoom
end

function Zoom:getLocalPoint(pt)
    return pt * self.zoom + self.center - self.offset * self.zoom
end

